//
// Created by gogoqiu on 2022/3/24.
//

#include "TestApp.h"
#include "../controls/InputDialog.h"
#include "../controls/ui.h"

#define APP_TAG "TestPreferenceApp"

struct MyAppSettingsQueryInterface{
    //eg:
    virtual wxString getQueryKeyOne() = 0 ;
    virtual bool getQueryKeyThree() = 0;
    virtual wxString getQueryKeyTwo() = 0;
    virtual wxString getQueryKeyFour() = 0;
};

/*
 * keys:
 */

//是否涵盖了数据对象
class MySettingsFrame :public BaseSettingsFrame, public MyAppSettingsQueryInterface{
private:
    void BuildView() override;
    void OnMenuHello( wxCommandEvent& event );
    void OnClickItem( wxCommandEvent& event );
    //wxScrolledWindow* m_listbox;
    BaseItem *lastItem;
protected:
    //void collectKeys();
public:
    MySettingsFrame( wxWindow *parent, const wxString &appId );
    //query key one;
    wxString getQueryKeyOne() override;
    bool getQueryKeyThree() override;
    wxString getQueryKeyFour() override;
    wxString getQueryKeyTwo() override;
    int setup() override;
    //void buildView() override;
    ~MySettingsFrame() override;
};

enum {
    ID_Hello
};
/*
 *
 */
MySettingsFrame::MySettingsFrame(wxWindow *parent, const wxString &appId) : BaseSettingsFrame(parent, appId) {
    //collectKeys();
    //BuildView();
    //setup first then build, setup to mix all values
    setup();
    BuildView();
}

/*
 * key, type, defaultValue, value, prompt, description
 * keys:
 *  keyOne  input   valueTwo    valueTwo    keyOne  keyOne
 *  keyTwo  input   valueOne    valueTwo    keyTwo  keyTwo
 *
 */
int MySettingsFrame::setup() {
    //fill the items
    BaseItem *item;
    item = new BaseItem( "keyOne", PT_INPUT, "valueOne", "valueOne", "keyOne", "keyOne" );
    items.push_back(item);
    item = new BaseItem( "keyTwo", PT_INPUT, "valueTwo", "valueTwo", "keyTwo", "keyTwo" );
    items.push_back(item);
    item = new BaseItem( "keyThree", PT_SWITCH, "true", "true", "keyThree", "keyThree" );
    items.push_back(item);
    wxString t = wxT("{"
                     "   \"chosen\": 1,"
                     "   \"choices\":[ \"selection1\", \"selection2\", \"selection3\" ]"
                     "   }");
    item = new BaseItem( "keyFour", PT_LIST, t,
                         t, "keyFour", "keyFour" );
    items.push_back(item);
    item = new BaseItem( "keyFive", PT_SWITCH, "true", "true", "keyFive", "keyFive" );
    items.push_back(item);
    lastItem = item;
    engine->Setup( &items );
    //BuildView();
    return 0;
}

wxString MySettingsFrame::getQueryKeyOne() {
    return wxString();
}

wxString MySettingsFrame::getQueryKeyTwo() {
    return wxString();
}

bool MySettingsFrame::getQueryKeyThree() {
    return false;
}

wxString MySettingsFrame::getQueryKeyFour() {
    return wxString();
}

MySettingsFrame::~MySettingsFrame() {
    for( auto *item: items )
        delete item;
}

void MySettingsFrame::OnMenuHello( wxCommandEvent& event ) {
    wxMessageBox( "1", "2", wxICON_ERROR );
}

void MySettingsFrame::BuildView() {
    SetClientSize(wxSize(350,500));
    auto *menuFile = new wxMenu;
    //
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    auto *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    auto *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    //动态绑定
    Bind(wxEVT_MENU, &MySettingsFrame::OnMenuHello, this, ID_Hello);
    BaseSettingsFrame::BuildView();
}

/*
 *
 */
void MySettingsFrame::OnClickItem(wxCommandEvent &event) {
    auto *myObj = reinterpret_cast<MyObj *>(event.GetEventUserData());
    //
    auto *baseItem = reinterpret_cast<BaseItem*>(myObj->data);
    switch( baseItem->getType() ){
        case PT_INPUT: {
            //
            auto *dialog = new InputDialog(this, baseItem);
            //show the dialog and collect the value
            if (dialog->ShowModal() == wxID_OK) {
                //
                UpdateItem(baseItem->getKey(), baseItem->getValue());
                //update View
                //
            }
            break;
        }
        case PT_SWITCH:

            break;
    }
    return;
}

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {
    //MyTagApp::OnInit();
    SetSettingId( APP_TAG );
    frame = new MySettingsFrame(nullptr, APP_TAG );
    //frame->Show();
    //frame->loadAll();
    //frame->setup();
    frame->Show();
    return true;
}

int MyApp::OnExit() {
    //delete frame;
    return wxAppBase::OnExit();
}
